@extends('layouts.admin')

@section('admin-content')
    <div class="box">
        @include('flash::message')
        <div class="box-header">
            <form action="{{ url("task/search") }}" method="POST">
                <div class="col-xs-3 col-sm-3">
                    <div class="form-group">
                        <select name="project_id" class="form-control">
                            <option value="">{{ trans("labels.project.name") }}</option>
                            @foreach($activeProjects as $project)
                                <option @if($project->id == $projectId) selected="selected" @endif value="{{ $project->id }}">{{ $project->name }}
                                    - {{ trans("strings.project.level.".$project->level) }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="col-xs-12 col-sm-8" style="padding-left: 0;margin-bottom: 10px;">
                    <div class="input-group">
                        <input type="text" name="title" class="form-control search-query"
                               placeholder="{{ trans("labels.task.title") }}"
                               value="{{ $title }}">
                        <span class="input-group-btn">
                            {{ csrf_field() }}
                            <button type="submit" class="btn btn-default btn-sm">
                                {{ trans("crud.search") }} <i class="glyphicon glyphicon-search"></i>
                            </button>
                        </span>
                    </div>
                </div>
            </form>
            <a class="btn btn-default btn-sm" href="{{url('task/select')}}">
                <i class="glyphicon glyphicon-pencil"></i>
                {{ trans("crud.create") }}
            </a>
        </div>
        <div class="box-body clearfix">
            <table class="table table-striped table-bordered table-hover">
                <tr>
                    <th>{{ trans("labels.user.name") }}</th>
                    <th>{{ trans("labels.project.name") }}</th>
                    <th>{{ trans("labels.task.title") }}</th>
                    <th>{{ trans("labels.task.created_at") }}</th>
                    <th>{{ trans("labels.task.status") }}</th>
                    <th>{{ trans("labels.action") }}</th>
                </tr>
                @foreach($paginator as $v)
                    <tr>
                        <td>{{ $v->user->name }}</td>
                        <td>{{ $v->project->name }} - {{ trans("strings.project.level.".$v->project->level) }}</td>
                        <td>{{ $v->title }}</td>
                        <td>{{ $v->created_at }}</td>
                        <td>{{ trans("strings.task.status.".$v->status) }}</td>
                        <td>
                            <div class="action-buttons">
                                <!-- 通过审核可以上线的任务-->
                                @if (\App\Repositories\TaskRepository::canDeploy($v->status))
                                    <a class="text-success" href="{{ url("task/deploy/".$v->id) }}">
                                        <i class="glyphicon glyphicon-cloud"></i>
                                        {{ trans("crud.online") }}
                                    </a>
                                @endif
                                <!-- 回滚的任务不能再回滚-->
                                @if ($v->status == config("constants.task.status.complete") && $v->enable_rollback == config("constants.task.enable_rollback.active"))
                                    <a class="btn-back text-warning" data-id="{{ $v->id }}" href="javascript:void(0);">
                                        <i class="glyphicon glyphicon-repeat"></i>
                                        {{ trans("crud.rollback") }}
                                    </a>
                                @endif
                                @if ($v->status != config("constants.task.status.complete"))
                                    <a class="btn-delete text-danger" href="javascript:void(0)">
                                        <i class="glyphicon glyphicon-trash"></i>
                                        {{ trans("crud.destory") }}
                                        <form action="{{url('task/'.$v->id)}}" method="POST" class="delete_task"
                                              style="display:none">
                                            <input type="hidden" name="_method" value="delete">
                                            {{ csrf_field() }}
                                        </form>
                                    </a>
                                @endif
                            </div>
                        </td>
                    </tr>
                @endforeach
            </table>
            {!! $paginator->render() !!}
        </div>
    </div>
@endsection

@section("admin-js")
    <script src="{{ asset('js/bootbox.js') }}"></script>
    <script type="text/javascript">
        $(function () {
            $('a.btn-delete').click(function () {
                var aobj = this;
                bootbox.confirm("{{ trans("alerts.deleteTitle") }}", function (result) {
                    if (result) {
                        $(aobj).find(".delete_task").submit();
                    }
                });
            });

            $('a.btn-back').click(function () {
                var tid = $(this).data("id");
                $.getJSON("{{ url("task/rollback") }}/" + tid, function (data) {
                    if (data.code != 0) {
                        bootbox.alert(data.msg);
                    } else {
                        window.location.href = "{{ url("task/deploy") }}/" + data.id;
                    }
                });
            });
        })
    </script>
@endsection
